package com.example.yuekaoceshi.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.yuekaoceshi.Main3Activity;
import com.example.yuekaoceshi.R;
import com.example.yuekaoceshi.adapter.RlvAdapter;
import com.example.yuekaoceshi.adapter.RlvAdapter2;
import com.example.yuekaoceshi.bean.Info;
import com.example.yuekaoceshi.http.DataBaseUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class CollectFragment extends Fragment {


    @BindView(R.id.rlv2)
    XRecyclerView mRlv2;
    private View view;
    private Unbinder unbinder;

    public CollectFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_collect, container, false);
        unbinder = ButterKnife.bind(this, inflate);

        return inflate;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }
    private void data() {
        final ArrayList<Info> list = (ArrayList<Info>) DataBaseUtils.getInstance().selectAll();
        //List<Info> list = DataBaseUtils.getInstance().selectAll();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRlv2.setLayoutManager(layoutManager);
        final RlvAdapter2 rlvAdapter2 = new RlvAdapter2(list);
        mRlv2.setAdapter(rlvAdapter2);
        //点击监听
        rlvAdapter2.setOnItemClickListener(new RlvAdapter2.OnItemClickListener() {
            @Override
            public void OnItemClick(View v, final int position) {
                AlertDialog alertDialog = new AlertDialog.Builder(getContext())
                        .setTitle("是否删除")
                        .setPositiveButton("取消", null)
                        .setNegativeButton("删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DataBaseUtils.getInstance().delect(list.get(position));
                                rlvAdapter2.notifyDataSetChanged();
                            }
                        })
                        .create();
                alertDialog.show();
            }
        });
        //长按监听
        rlvAdapter2.SetOnItemLongClickListener(new RlvAdapter2.OnItemClickListener() {
            @Override
            public void OnItemClick(View v, int position) {
                Intent intent = new Intent(getContext(), Main3Activity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            data();
        }
    }
}
